package com.cloud.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.system.entity.SysMonitor;
import com.cloud.system.service.ISysMonitorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 服务监控配置
 *
 * @author yzj
 * @since 2022-10-25
 */
@Tag(name = "服务监控配置")
@RestController
@RequestMapping("/system/monitor")
public class SysMonitorController extends BaseController {

    @Autowired
    private ISysMonitorService sysMonitorService;

    @Operation(summary = "服务监控配置分页")
    @RequestPermissions("system:monitor:list")
    @GetMapping("/list")
    public Result list(SysMonitor sysMonitor) {
        startPage();
        List<SysMonitor> list = sysMonitorService.selectSysMonitorList(sysMonitor);
        return getDataTable(list);
    }

    @Operation(summary = "查询项目应用服务列表")
    @GetMapping("/allList")
    public Result allList() {
        return Result.success("查询成功", sysMonitorService.selectProjectMonitorList());
    }

    @Operation(summary = "新增监控配置信息")
    @Logs(title = "新增监控配置", businessType = BusinessType.INSERT)
    @RequestPermissions("system:monitor:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysMonitor sysMonitor) {
        return toResult(sysMonitorService.insertObject(sysMonitor));
    }

    @Operation(summary = "修改监控配置信息")
    @Logs(title = "修改监控配置", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:monitor:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysMonitor sysMonitor) {
        return toResult(sysMonitorService.updateObject(sysMonitor));
    }

    @Operation(summary = "查询监控配置详情")
    @RequestPermissions("system:monitor:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        return Result.success("查询成功", sysMonitorService.selectMonitorinfo(id));
    }

    @Operation(summary = "删除监控配置信息")
    @Logs(title = "删除监控配置", businessType = BusinessType.DELETE)
    @RequestPermissions("system:monitor:remove")
    @PostMapping("/remove")
    public Result remove(String ids) {
        List<String> list = Arrays.asList(ids.split(StringPool.COMMA));
        return toResult(sysMonitorService.removeByIds(list));
    }

    @Operation(summary = "修改监控配置启用状态")
    @Parameters({
            @Parameter(name = "status", description = "启用状态（1启用 0停用）"),
            @Parameter(name = "monitorId", description = "监控配置主键")
    })
    @Logs(title = "修改监控配置启用状态", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:monitor:status")
    @PostMapping("/updateStatus")
    public Result updateStatus(String status, Long monitorId) {
        return toResult(sysMonitorService.updateServiceStatus(status, monitorId));
    }

}
